<?php
use src\api\src\CorpApi;
use src\api\datastructure\message\Message;
use src\api\datastructure\message\TextMessageContent;
use src\api\datastructure\message\TextCardMessageContent;

class Wework {

	// 企业ID
	protected $corpid = null;

	// 应用ID
	protected $agentid = null;

	// 应用密钥
	protected $secret = null;

	/**
	 *
	 * @var CorpApi
	 */
	protected $client = null;

	function __construct() {
		$this->corpid = config('wework.corpid');
		$this->agentid = intval(config('wework.agentid'));
		$this->secret = config('wework.secret');

		$this->client = new CorpApi($this->corpid, $this->agentid, $this->secret);
	}

	/**
	 * 发送文本消息
	 *
	 * @author KING
	 * @date 2020年5月22日
	 *
	 * @param string $message
	 * @param array $userArray		企业微信用户ID
	 * @param array $partyArray		企业微信部门ID
	 * @param array $tagArray		企业微信标签ID
	 *
	 * @return array {
		   "errcode" : 0,
		   "errmsg" : "ok",
		   "invaliduser" : "userid1|userid2",
		   "invalidparty" : "partyid1|partyid2",
		   "invalidtag": "tagid1|tagid2"
		 }
	 */
	public function textMessageSend($message, $userArray=[], $partyArray=[], $tagArray=[]) {
		try {
			$messageObj = new Message();
			$messageObj->agentid=$this->agentid;
			$messageObj->touser = is_array($userArray) ? $userArray : [$userArray];
			$messageObj->toparty = is_array($partyArray) ? $partyArray : [$partyArray];
			$messageObj->totag = is_array($tagArray) ? $tagArray : [$tagArray];
			$messageObj->messageContent = new TextMessageContent($message);
			$res = $this->client->MessageSend($messageObj);
			return $this->client->rspJson;
		} catch (\Exception $e) {
			throw new \Exception($e->getMessage(), $e->getCode());
		}
	}

	/**
	 * 发送卡片消息
	 *
	 * @author KING
	 * @date 2020年5月22日
	 *
	 * @param string $title
	 * @param string $description
	 * @param string $url
	 * @param string $btnText
	 * @param array $userArray		企业微信用户ID
	 * @param array $partyArray		企业微信部门ID
	 * @param array $tagArray		企业微信标签ID
	 *
	 * @return array {
	 "errcode" : 0,
	 "errmsg" : "ok",
	 "invaliduser" : "userid1|userid2",
	 "invalidparty" : "partyid1|partyid2",
	 "invalidtag": "tagid1|tagid2"
	 }
	 */
	public function cardMessageSend($title, $description, $url, $btnText, $userArray=[], $partyArray=[], $tagArray=[]) {
		try {
			$msgContentObj = new TextCardMessageContent();
			$msgContentObj->title = $title;
			$msgContentObj->url = $url;
			$msgContentObj->description = $description;
			$msgContentObj->btntxt = $btnText;

			$messageObj = new Message();
			$messageObj->agentid=$this->agentid;
			$messageObj->touser = is_array($userArray) ? $userArray : [$userArray];
			$messageObj->toparty = is_array($partyArray) ? $partyArray : [$partyArray];
			$messageObj->totag = is_array($tagArray) ? $tagArray : [$tagArray];
			$messageObj->messageContent = $msgContentObj;
			$res = $this->client->MessageSend($messageObj);
			return $this->client->rspJson;
		} catch (\Exception $e) {
			throw new \Exception($e->getMessage(), $e->getCode());
		}
	}

	/**
	 * 获取部门用户列表
	 *
	 * @author KING
	 * @date 2020年5月22日
	 *
	 * @param integer $departmentId	部门ID
	 * @param integer $fetchChild	是否递归获取所有下级
	 *
	 * @return Array(
				    [errcode] => 0
				    [errmsg] => ok
				    [userlist] => Array(
				            [0] => Array(
				                    [userid] => LiuLingFeng
				                    [name] => 刘凌风
				                    [department] => Array(
				                            [0] => 1
				                        )
				                    [position] =>
				                    [mobile] => 15626188630
				                    [gender] => 1
				                    [email] => 909213473@qq.com
				                    [avatar] => http://wework.qpic.cn/bizmail/nKnZRqoKdFKCib0j5qVanrmZicC9eicRxLlfTkF9mfd4FibCwfu4TA03Gw/0
				                    [status] => 1
				                    [enable] => 1
				                    [isleader] => 0
				                    [extattr] => Array(
				                    		[attrs] => Array()
				                        )
				                    [hide_mobile] => 0
				                    [telephone] =>
				                    [order] => Array(
				                            [0] => 0
				                        )
				                    [main_department] => 1
				                    [qr_code] => https://open.work.weixin.qq.com/wwopen/userQRCode?vcode=vc479551178e402d54
				                    [alias] =>
				                    [is_leader_in_dept] => Array(
				                            [0] => 0
				                        )
				                    [thumb_avatar] => http://wework.qpic.cn/bizmail/nKnZRqoKdFKCib0j5qVanrmZicC9eicRxLlfTkF9mfd4FibCwfu4TA03Gw/100
				                )
				        )
				)
	 */
	public function userList($departmentId=1, $fetchChild=1) {
		try{
			return $this->client->UserList($departmentId, $fetchChild);
		} catch (\Exception $e) {
			throw new \Exception($e->getMessage(), $e->getCode());
		}
	}

	/**
	 * 创建菜单
	 *
	 * @author KING
	 * @date 2020年6月13日
	 *
	 * @param array $buttonArray
	 * @throws \Exception
	 * @return unknown
	 */
	public function memuCreate($buttonArray) {
		try{
			return $this->client->MenuCreate($buttonArray);
		} catch (\Exception $e) {
			throw new \Exception($e->getMessage(), $e->getCode());
		}
	}
}
?>